<?php $this->layout = "//layouts/column1"; ?>
<h3 style="border-bottom: 1px solid #ddd; padding-bottom: 5px">Administrator Dashboard</h3>
<div style="font-size: 15px">
    <fieldset class="details span-10 vi-fieldset" style="border-color: #009FC4;">
    <legend class="vi-legend" style="border-color: #009FC4; background-color: #87D7A7;">Member Count ( Based on Status )</legend>
    <table>
        <tbody>
            <tr>
                <td>Total Members:</td><td><?php echo $total_member ?></td>
            </tr>
            <tr>
                <td>Active Members:</td><td><?php echo $active_member ?></td>
            </tr>
            <tr>
                <td>Blocked Members:</td><td><?php echo $block_member ?></td>
            </tr>
            <tr>
                <td>Suspended Members:</td><td><?php echo $suspended_member ?></td>
            </tr>
        </tbody>    	
    </table>
</fieldset>    
<fieldset class="details span-11 right vi-fieldset" style="border-color: #009FC4;">
    <legend class="vi-legend" style="border-color: #009FC4; background-color: #87D7A7;">Member Count ( Based on Deposit Status )</legend>
    <table>
        <tbody>
            <tr>
                <td>Total Members who deposited:</td><td><?php echo $deposited ?></td>
            </tr>
            <tr>
                <td>Total Members who have not deposited:</td><td><?php echo $not_deposited ?></td>
            </tr>
        </tbody>    	
    </table>
</fieldset>
<!--
<fieldset class="details span-11 right">
    <legend>Balance Status</legend>
    <div class="" style="font-style: italic">
        Your current balance is : <strong><?php echo $balance ?>
    </div>

</fieldset>
-->
<div class="clear"></div>
<br />
<h3 style="border-bottom: 1px solid #ddd; padding-bottom: 5px">System Earnings</h3>
<fieldset class="details span-10 vi-fieldset" style="border-color: #009FC4;">
    <legend class="vi-legend" style="border-color: #009FC4; background-color: #87D7A7;">Ego pay</legend>
    <table>
        <tr>
            <th></th>
            <th>Today</th>
            <th>This Week</th>
            <th>This Month</th>
            <th>Total</th>
        </tr>	
        <tbody>
            <tr>
                <td>Deposit</td>
                <td><?php echo getDeposit("T", "D", "EP"); ?></td>
                <td><?php echo getDeposit("TW", "D", "EP"); ?></td>
                <td><?php echo getDeposit("TM", "D", "EP"); ?></td>
                <td><?php echo getTotalDeposit("EP"); ?></td>
            </tr>
            <tr>
                <td>Withdraw</td>
                <td><?php echo getWithdraw("T", "EP"); ?></td>
                <td><?php echo getWithdraw("TW","EP"); ?></td>
                <td><?php echo getWithdraw("TM", "EP"); ?></td>
                <td><?php echo getTotalWithdraw("EP"); ?></td>
            </tr>
        </tbody>    	
    </table>
</fieldset>
<div class="clear" style="height: 30px"></div>

<fieldset class="details span-10 vi-fieldset " style="border-color: #009FC4;">
    <legend class="vi-legend" style="border-color: #009FC4; background-color: #87D7A7;">Perfect Money</legend>
    <table>
        <tr>
            <th></th>
            <th>Today</th>
            <th>This Week</th>
            <th>This Month</th>
            <th>Total</th>
        </tr>	
        <tbody>
            <tr>
                <td>Deposit</td>
                <td><?php echo getDeposit("T", "D", "PM"); ?></td>
                <td><?php echo getDeposit("TW", "D", "PM"); ?></td>
                <td><?php echo getDeposit("TM", "D", "PM"); ?></td>
                <td><?php echo getTotalDeposit("PM"); ?></td>
            </tr>
            <tr>
                <td>Withdraw</td>
                <td><?php echo getWithdraw("T", "PM"); ?></td>
                <td><?php echo getWithdraw("TW", "PM"); ?></td>
                <td><?php echo getWithdraw("TM", "PM"); ?></td>
                <td><?php echo getTotalWithdraw("PM"); ?></td>
            </tr>
        </tbody>    	
    </table>
</fieldset>

<div class="clear" style="height: 30px"></div>
<fieldset class="details span-11 right vi-fieldset" style="border-color: #009FC4;">
    <legend class="vi-legend" style="border-color: #009FC4; background-color: #87D7A7;">Total</legend>
    <table>
        <tr>
            <th></th>
            <th>Today</th>
            <th>This Week</th>
            <th>This Month</th>
            <th>Total</th>
        </tr>	
        <tbody>
            <tr>
                <td>Deposit</td>
                <td><?php echo getDeposit("T", "D", ""); ?></td>
                <td><?php echo getDeposit("TW", "D", ""); ?></td>
                <td><?php echo getDeposit("TM", "D", ""); ?></td>
                <td><?php echo getTotalDeposit(""); ?></td>
            </tr>
            <tr>
                <td>Withdraw</td>
                <td><?php echo getWithdraw("T", ""); ?></td>
                <td><?php echo getWithdraw("TW", ""); ?></td>
                <td><?php echo getWithdraw("TM", ""); ?></td>
                <td><?php echo getTotalWithdraw(""); ?></td>
            </tr>
        </tbody>    	
    </table>
</fieldset>

<div class="clear"></div>
</div>
<?php
/* action = today/this week/ this month.
 * type = debit or credit.
 * id = payment method code. 
 */

function getDeposit($action, $type, $code) {
    $fd = date('Y-m-d');
    $ld = date('Y-m-d');

    $start = date("Y-m-d H:i:s");
    $year = date('Y', strtotime($start));
    $week = date('W', strtotime($start));
    $sunday = strtotime($year . 'W' . $week . '0');

    if ($action == "TW") {
        /* Start from sunday | this week calculation */
        $fd = date('Y-m-d', strtotime('+0 Days', $sunday));
        $ld = date('Y-m-d', strtotime('+6 Days', $sunday));
    } else if ($action == "TM") {
        /* this month calculation */
        $fd = date('Y-m-01');
        $ld = date('Y-m-t');
    }

    $method = "credit";
    if ($type == "D") {
        $method = "debit";
    }
    $superid = Member::model()->find("member_type=:member_type", array('member_type' => LevelLookUp::SUPERADMIN))->member_id;
//    $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id = $superid 
//			 and substr(transaction_date,1,10)>='$fd' and substr(transaction_date,1,10)<='$ld'
//			 and payment_processors_id is not null";
    $sql = "SELECT sum($method) sum FROM member_transaction_history where isDeposit = '1'
			 and substr(transaction_date,1,10)>='$fd' and substr(transaction_date,1,10)<='$ld'";

    $record = PaymentProcessor::model()->findByAttributes(array('code' => $code));
    if ($record != null) {
        $sql = $sql . " and payment_processors_id = $record->payment_processors_id";
    }
    $command = Yii::app()->db->createCommand($sql);
    $sum = $command->queryScalar();
    if (empty($sum)) {
        $sum = 0;
    }
    return $sum;
}

function getWithdraw($action, $code) {
    $fd = date('Y-m-d');
    $ld = date('Y-m-d');

    $start = date("Y-m-d H:i:s");
    $year = date('Y', strtotime($start));
    $week = date('W', strtotime($start));
    $sunday = strtotime($year . 'W' . $week . '0');

    if ($action == "TW") {
        /* Start from sunday | this week calculation */
        $fd = date('Y-m-d', strtotime('+0 Days', $sunday));
        $ld = date('Y-m-d', strtotime('+6 Days', $sunday));
    } else if ($action == "TM") {
        /* this month calculation */
        $fd = date('Y-m-01');
        $ld = date('Y-m-t');
    }

    $method = "debit";
    
    $sql = "SELECT sum($method) sum FROM member_transaction_history where isWithdraw = '1'
			 and substr(transaction_date,1,10)>='$fd' and substr(transaction_date,1,10)<='$ld'";

    $record = PaymentProcessor::model()->findByAttributes(array('code' => $code));
    if ($record != null) {
        $sql = $sql . " and payment_processors_id = $record->payment_processors_id";
    }
    $command = Yii::app()->db->createCommand($sql);
    $sum = $command->queryScalar();
    if (empty($sum)) {
        $sum = 0;
    }
    return $sum;
}

function getTotalDeposit($code) {
    $method = "debit";
    $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id is not null   
			 and isDeposit = '1'";

    $record = PaymentProcessor::model()->findByAttributes(array('code' => $code));
    if ($record != null) {
        $sql = $sql . " and payment_processors_id = $record->payment_processors_id";
    }
    $command = Yii::app()->db->createCommand($sql);
    $sum = $command->queryScalar();
    if (empty($sum)) {
        $sum = 0;
    }
    return $sum;
}

function getTotalWithdraw($code) {
    $method = "debit";
    $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id is not null   
			 and isWithdraw = '1'";

    $record = PaymentProcessor::model()->findByAttributes(array('code' => $code));
    if ($record != null) {
        $sql = $sql . " and payment_processors_id = $record->payment_processors_id";
    }
    $command = Yii::app()->db->createCommand($sql);
    $sum = $command->queryScalar();
    if (empty($sum)) {
        $sum = 0;
    }
    return $sum;
}

function getTotalTransaction($type, $code) {
    $method = "credit";
    if ($type == "D") {
        $method = "debit";
    }
    $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id is not null   
			 and payment_processors_id is not null";

    $record = PaymentProcessor::model()->findByAttributes(array('code' => $code));
    if ($record != null) {
        $sql = $sql . " and payment_processors_id = $record->payment_processors_id";
    }
    $command = Yii::app()->db->createCommand($sql);
    $sum = $command->queryScalar();
    if (empty($sum)) {
        $sum = 0;
    }
    return $sum;
}
?>
//<?php
//
///*
//
//This script demonstrates querying account history
//using PerfectMoney API interface.
//
//*/
//
//// trying to open URL
//$f=fopen('https://perfectmoney.is/acct/historycsv.asp?startmonth=1&startday=1&startyear=2014&endmonth=1&endday=4&endyear=2014&AccountID=9435874&PassPhrase=viewinvesta205', 'rb');
//
//if($f===false){
//   echo 'error openning url';
//}
//
//// getting data to array (line per item)
//$lines=array();
//while(!feof($f)) array_push($lines, trim(fgets($f)));
//
//fclose($f);
//
//// try parsing data to array
//if($lines[0]!='Time,Type,Batch,Currency,Amount,Fee,Payer Account,Payee Account,Memo'){
//
//   // print error message
//   echo $lines[0];
//
//}else{
//
//   // do parsing
//   $ar=array();
//   $n=count($lines);
//   for($i=1; $i<$n; $i++){
//
//      $item=explode(",", $lines[$i], 9);
//      if(count($item)!=9) continue; // line is invalid - pass to next one
//      $item_named['Time']=$item[0];
//      $item_named['Type']=$item[1];
//      $item_named['Batch']=$item[2];
//      $item_named['Currency']=$item[3];
//      $item_named['Amount']=$item[4];
//      $item_named['Fee']=$item[5];
//      $item_named['Payer Account']=$item[6];
//      $item_named['Payee Account']=$item[7];
//      $item_named['Memo']=$item[8];
//      array_push($ar, $item_named);
//   }
//
//}
//
//echo '<pre>';
//print_r($ar);
//echo '</pre>';
//
//?>